package logicaPresentacion;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import logicaNegocio.Aula;
import logicaNegocio.GestionAdministrador;


public class VerAulas extends JInternalFrame implements ActionListener, Observer
{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private GestionAdministrador gAd;
	private JTable tbAulas;
	private JScrollPane scrollPane; 
    private JPanel jpBotones;		
	private String eleccion;
	private TablaAulas tabla = null;  
	private JButton aceptar, cancelar, borrar;
	private ClaseAObservar observable;
	private ArrayList<Aula> aulas;
	private Aula a;
		
	public VerAulas(GestionAdministrador gAd,String eleccion, ClaseAObservar observable)
	{
		
		super("Aulas", 
	              true, 
	              false, 
	              true, 
	              true);
		setVisible(true);
		setSize(800,300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.inicializarSubComponentes();
		this.eleccion = eleccion;
		this.gAd = gAd;
		this.observable = observable;
		blnCargarFormulario();
	}
	
	public void blnCargarFormulario()
	{
		crearTablaAulas();
		this.setLocation(200,200);
		this.setVisible(true);
		inicializarSubComponentes();
	}
	
	public void crearTablaAulas()
	{
		
		tbAulas=null;
		tabla=new TablaAulas();
		tabla.crearTabla(gAd.getAulas());
		tbAulas = new JTable(tabla);
		tbAulas.setPreferredScrollableViewportSize(new Dimension(500, 70));
		tbAulas.setFillsViewportHeight(true);
		tbAulas.setEnabled(true);
		tbAulas.setRowSelectionAllowed(true);
				
	}
	public void inicializarSubComponentes()
	{			
		try
		{				
			scrollPane= new JScrollPane(tbAulas);

			this.getContentPane().setLayout(new BorderLayout());
	        this.getContentPane().add(scrollPane,"Center");
	        
	        Box boxbotones;
	        if(eleccion.equals("Borrar"))
	        {
	        	borrar=new JButton("Borrar");
	        	borrar.addActionListener(this);
				cancelar=new JButton("Cancelar");
				cancelar.addActionListener(this);
		      
		        boxbotones = Box.createHorizontalBox();
		        boxbotones.add(borrar);
		        Box.createHorizontalStrut(20);
		        boxbotones.add(cancelar);
		        
		        jpBotones=new JPanel();
				jpBotones.setLayout(new FlowLayout());
				jpBotones.add(boxbotones);
	        }
	        else
	        {
	        
		        aceptar=new JButton("Aceptar");
				aceptar.addActionListener(this);
				jpBotones=new JPanel();
				jpBotones.setLayout(new FlowLayout());
				jpBotones.add(aceptar);
	        }
			
			
			this.getContentPane().add(jpBotones,"South");
		}
		catch(Exception e)
		{

		}
		
	}
		
	public void actionPerformed(ActionEvent e)
	{	
		if (e.getSource() instanceof JButton) 
		{
			JButton btnPulsado=(JButton)e.getSource();

			if ((btnPulsado==aceptar)||(btnPulsado==cancelar)) 
			{
				dispose();
			}
			if(btnPulsado==borrar)
			{
				int row = tbAulas.getSelectedRow();
				if(row>=0)
				{
					aulas = gAd.getAulas();
					
					a = aulas.get(row);//4 es el dni
					gAd.borrarAula(a);

					observable.setTabla();
					
					JOptionPane.showMessageDialog(null, "Tupla Borrada");
					dispose();
				}
				else
				{
					JOptionPane.showMessageDialog(null, "Debe seleccionar una fila si quiere borrar una tupla");          
				}
			}						
		}
	}

	public void update(Observable o, Object arg) 
	{
		tbAulas=null;
		tabla.crearTabla(gAd.getAulas());
		tbAulas = new JTable(tabla);
		tbAulas.setPreferredScrollableViewportSize(new Dimension(500, 70));
		tbAulas.setFillsViewportHeight(true);
		tbAulas.setEnabled(true);
		tbAulas.setRowSelectionAllowed(true);
		
	}
	
 class TablaAulas extends AbstractTableModel
	 {
		 /**
		 * 
		 */
		private static final long serialVersionUID = 6056384139002577999L;
		private String[] columnNames = {"ID_Aula", "Numero de Aula","Aforo", "Campus"};
		private Object[][] data;
	        
        public void crearTabla(ArrayList<Aula> aulas)
        {
        	
    		int filas = aulas.size();
    		data=new Object[filas][];

			if (eleccion == "Aforo")
    		{
    			aulas = gAd.ordenarAulas(aulas);
    		}
			
    		for (int cont = 0;  cont<aulas.size();cont++)
    		{
    			Aula al = (Aula) aulas.get(cont);
    			Object[]a={
    					   new String(Integer.toString(al.getID())),
    					   new String (Integer.toString(al.getNumeroAula())),
    					   new Integer(al.getAforoAula()),
    					   new String(al.getCampus().getNombreCampus()),
    			};
    			data[cont]=a;
    		}	    			        	
        }
        
       public int getColumnCount() 
        {
            return columnNames.length;
        }

        public int getRowCount() {
            return data.length;
        }

        public String getColumnName(int col) 
        {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) 
        {
            return data[row][col];
        } 
    }
}
